﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMS.ENT;
using PMS.DAL;
using System.IO;
using System.Security.Cryptography;
using System.Runtime.InteropServices.ComTypes;

namespace PMS.BLL
{
    public class FileBLL
    {
        #region Get
        public static FileENT GetFileByID(int fileID)
        {
            try
            {
                return FileDAL.GetFileByID(fileID);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static FileCollection GetFileList(string fileName, int projectID, int scheduleID, int userID, string status, string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                return FileDAL.GetFileList(fileName, projectID, scheduleID, userID, status, orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static FileCollection GetFileListForCMB(int userID, int projectID)
        {
            try
            {
                FileCollection listFile = new FileCollection();
                listFile.Add(new FileENT { FileID = 0, FileName = string.Empty });
                listFile.AddRange(FileDAL.GetFileListForCMB(userID, projectID));
                return listFile;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static FileCollection GetFileListByStatementID(int statementID)
        {
            try
            {
                return FileDAL.GetFileListByStatementID(statementID);
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region Insert Update Delete Confirm

        public static int InsertFile(FileENT file)
        {
            try
            {
                return FileDAL.InsertFile(file);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int UpdateFile(FileENT file)
        {
            try
            {
                return FileDAL.UpdateFile(file);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int DeleteFile(string fileID, int userID)
        {
            try
            {
                return FileDAL.DeleteFile(fileID, userID);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int ConfirmFile(string fileID, int userID)
        {
            try
            {
                return FileDAL.ConfirmFile(fileID, userID);
            }
            catch (Exception)
            {

                throw;
            }
        }


        #endregion

        #region FileMD5
        public static string GetMD5HashFromFile(string fileName)
        {
            FileStream file = new FileStream(fileName, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        #endregion

        #region ReadWordFile
        public static string ReadMsWord(string filePath)
        {
            try
            {
                string outText = string.Empty;
                // create word application

                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

                // create object of missing value

                object miss = System.Reflection.Missing.Value;

                // create object of selected file path

                object path = filePath;

                // set file path mode

                object readOnly = false;
                object encode = Encoding.Unicode;
                // open document                

                Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

                // select whole data from active window document

                docs.ActiveWindow.Selection.WholeStory();

                // handover the data to cllipboard

                docs.ActiveWindow.Selection.Copy();

                //set data into richtextbox control in text format

                outText = docs.Content.Text;

                // close the document

                docs.Close(ref miss, ref miss, ref miss);

                word.Quit(ref miss, ref miss, ref miss);

                return outText;

            }

            catch (Exception ex) { throw; }



        }

       

        #endregion


    }
}
